package com.atguigu.lease.web.admin.service.impl;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.atguigu.lease.common.busiexception.BusiException;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private FeeKeyMapper feeKeyMapper;

    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private GraphInfoService  graphInfoService;

    @Override
    public IPage<ApartmentItemVo> getApartmentItemVoPage(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.getApartmentItemVoPage(page, queryVo);
    }

    @Override
    public ApartmentDetailVo getApartmentDetailVoById(Long id) {
//      todo 根据id 获取ApartmentDetailVo 除了要查询到 ApartmentInfo  还需要查询公寓配套 公寓标签 公寓杂费 图片 然后存到vo里
//          所以单表无法满足，需要自定义方法去service或者mapper里面实现
//        todo 1、先调用自己service 的list方法 根据id查询所有的ApartmentInfo
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        ApartmentInfo apartmentInfo = this.getById(id);
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
//        todo 2、查询公寓配套
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.getFacilityListByApartmentId(id);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
//        todo 3、查询公寓标签
        List<LabelInfo> labelInfoList = labelInfoMapper.getLableListByApartmentId(id);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
//        todo 4、查询公寓杂费
        List<FeeValueVo> feeValueVoList = feeValueMapper.getFeeValueVoListByApartmentId(id);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
//        todo 5、查询图片
        List<GraphVo> graphVoList = graphInfoService.getGraphVoListByApartmentId(id);
        apartmentDetailVo.setGraphVoList(graphVoList);
        return apartmentDetailVo;
    }

    @Override
    public void saveOrUpdateApartmentSubmitVo(ApartmentSubmitVo apartmentSubmitVo) {
//        todo 如果 vo里不带id 就是新增  如果带id就是修改
        boolean idNotNull = apartmentSubmitVo.getId() != null;
//        todo  如果不为空就说明是改，把基本信息先修改保存
        this.saveOrUpdate(apartmentSubmitVo);
        if(idNotNull){
//        todo 因为不知道是修改的什么数据 所以把所有中间表数据全都清了  后面一律当新增对待  ApartmentSubmitVo

//            private List<Long> facilityInfoIds;
            apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>()
                    .eq(ApartmentFacility::getApartmentId,apartmentSubmitVo.getId()));
//            private List<Long> labelIds;
            apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>()
                    .eq(ApartmentLabel::getApartmentId,apartmentSubmitVo.getId()));
//            private List<Long> feeValueIds;
            apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>()
                    .eq(ApartmentFeeValue::getApartmentId,apartmentSubmitVo.getId()));
//            private List<GraphVo> graphVoList;
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>()
                    .eq(GraphInfo::getItemType, ItemType.APARTMENT)
                    .eq(GraphInfo::getItemId,apartmentSubmitVo.getId()));
         }

//        todo 无论是修改还是新增 都需要重新获取 四个中间表
//        private List<Long> facilityInfoIds;
//        todo 填充公寓配套
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        ArrayList<ApartmentFacility> apartmentFacilities = new ArrayList<>();
        facilityInfoIds.forEach(facilityInfoId -> {
            ApartmentFacility apartmentFacility = ApartmentFacility.builder()
                    .apartmentId(apartmentSubmitVo.getId())
                    .facilityId(facilityInfoId)
                    .build();
            apartmentFacilities.add(apartmentFacility);
        });
        apartmentFacilityService.saveBatch(apartmentFacilities);

//        批量增加
//        todo 填充标签
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>();
        labelIds.forEach(labelId -> {
            ApartmentLabel apartmentLabel = ApartmentLabel.builder()
                    .apartmentId(apartmentSubmitVo.getId())
                    .labelId(labelId)
                    .build();
            apartmentLabels.add(apartmentLabel);
        });
        apartmentLabelService.saveBatch(apartmentLabels);
//     todo 填充杂费
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
        feeValueIds.forEach(feeValueId -> {
            ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder()
                    .apartmentId(apartmentSubmitVo.getId())
                    .feeValueId(feeValueId)
                    .build();
            apartmentFeeValues.add(apartmentFeeValue);
        });
        apartmentFeeValueService.saveBatch(apartmentFeeValues);

//        todo 填充图片
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        ArrayList<GraphInfo> graphInfos = new ArrayList<>();
        graphVoList.forEach(graphVo -> {
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setItemId(apartmentSubmitVo.getId());
            graphInfo.setItemType(ItemType.APARTMENT);
            graphInfo.setUrl(graphVo.getUrl());
            graphInfo.setName(graphVo.getName());

            graphInfos.add(graphInfo);
        });
        graphInfoService.saveBatch(graphInfos);
    }

    @Override
    public void deleteRoomByApartmentId(Long id) {
        if(id != null){
            throw new BusiException(500,"删除失败，id不能为空");
        }
//        todo 1、删除公寓基本信息
        this.removeById(id);
//        todo 2、根据id删除四个中间表
//        配套信息表
        apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>()
                .eq(ApartmentFacility::getApartmentId,id));
//        标签信息表
        apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>()
                .eq(ApartmentLabel::getApartmentId,id));
//        杂费信息表
        apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>()
                .eq(ApartmentFeeValue::getApartmentId, id));
//        图片信息表
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>()
                .eq(GraphInfo::getItemId,id));
    }


}




